package com.websocket.demo.controller;

import com.websocket.demo.server.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author lirenchun
 */
@RestController
public class ServerController {
    @Autowired
    private WebSocketServer webSocketServer;

    @RequestMapping(value = "/server", method = {RequestMethod.POST, RequestMethod.GET})
    public void server(HttpServletRequest request) throws IOException {
        try {
            String msg = request.getParameter("msg");
            String user = request.getParameter("user");
            webSocketServer.sendToUserByUserName(user, msg);
        } catch (Exception e) {
            System.out.println(e.toString());
        }
    }
}
